<?
function get_data($url)
{
  $ch = curl_init();
  $timeout = 5;
  curl_setopt($ch,CURLOPT_URL,$url);
  curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
  $data = curl_exec($ch);
  curl_close($ch);
  return $data;
}
include('inc/addondb.php');

if(isset($_GET['id'])){
if($_GET['step'] == "skip"){
mysql_query("INSERT INTO `ytsubed` (user_id, site_id) VALUES('{$_GET['usr']}', '{$_GET['id']}')");
echo "<table cellpadding=\"2\" cellspacing=\"0\" width=\"500\" align=\"center\" class=\"mainfocus\"><tr><td align=\"center\"><font size=\"3\"><b>Done!</b></font></td></tr></table>";
}else{
$dbres1	= mysql_query("SELECT * FROM `ytsubs` WHERE `user`='{$_GET['usr']}'");
$data3 = mysql_fetch_object($dbres1);
$site = mysql_fetch_object(mysql_query("SELECT * FROM `ytsubs` WHERE `id`='{$_GET['id']}'"));



$url  = get_data("https://gdata.youtube.com/feeds/api/events?v=2&author=".$data3->y_name."&key=AI39si4nAlyji81NMcQxsgSu9kB46Hdh8gGYAx6vk2XOnglVjZBVtlG0wo7bdf5Dx_UVzXPog-Aq9i40ss-TBQ65OOSvqp_oXQ&fields=link,entry(yt:username)");
$xml = new SimpleXMLElement($url); 
$yt = $xml->entry->children( 'http://gdata.youtube.com/schemas/2007' );

$plused1 = mysql_query("SELECT * FROM `ytsubed` WHERE `site_id`='{$_GET['id']}' AND `user_id`='{$_GET['usr']}'");
$plused  = mysql_num_rows($plused1);
$tw1 = mysql_query("SELECT * FROM `ytsubs` WHERE `id`='{$_GET['id']}' AND `active`='0'");
$tw = mysql_fetch_object($tw1);
$tw22 = mysql_query("SELECT * FROM `ytsubs` WHERE `id`='{$_GET['id']}'");
$tw2 = mysql_fetch_object($tw22);
if ($yt->username <> $site->y_name) { 
 echo "<table cellpadding=\"2\" cellspacing=\"0\" width=\"500\" align=\"center\" class=\"mainfocus\"><tr><td align=\"center\"><font size=\"3\"><b>Error!</b></font></td></tr><tr><td><center><b>We can't verify your Subscription! </b></center></td></tr></table>";
}else if($plused > 0){
echo "<table cellpadding=\"2\" cellspacing=\"0\" width=\"500\" align=\"center\" class=\"mainfocus\"><tr><td align=\"center\"><font size=\"3\"><b>Error!</b></font></td></tr><tr><td><center><b>You already Subscribed to ".$site->y_name."!</b></center></td></tr></table>";
}else if ($yt->username = $site->y_name) {    
mysql_query("UPDATE `users` SET `coins`=`coins`+'$tw->cpc' WHERE `id`='{$_GET['usr']}'");
mysql_query("UPDATE `ytsubs` SET `earned`=`earned`+'1' WHERE `user`='$tw->user'");
mysql_query("UPDATE `ytsubs` SET `points`=`points`'$tw->cpc' WHERE `id`='$tw->user'");
mysql_query("INSERT INTO `ytsubed` (user_id, site_id) VALUES('{$_GET['usr']}', '{$_GET['id']}')");

echo "<table cellpadding=\"2\" cellspacing=\"0\" width=\"500\" align=\"center\" class=\"mainfocus\"><tr><td align=\"center\"><font size=\"3\"><b>Success!</b></font></td></tr><tr><td><center><b>You Subscribed to ".$site->y_name."!<br><font color=\"#d4a1ff\" size=\"3\">You got ".$tw->cpc." Coins!</font></center></td></tr></table>";
}else{
echo "<table cellpadding=\"2\" cellspacing=\"0\" width=\"500\" align=\"center\" class=\"maintable\"><tr><td><center><b>Youtube says you are not Subscribed to ".$site->y_name."!</b><br><i>Check again if you are logged into " . $data3->y_name . " on Youtube.</i><br><b>Try Confirming again or Skip them!</b></center></td></tr></table><br>";
}
}}
?>